<?php
/**
 * Created by PhpStorm.
 * User: Dominik
 * Date: 3/26/14
 * Time: 3:13 PM
 */

include "connection.php";

class sqlManager {

    function isUserNameUsed($login){
        $query = "SELECT `username` FROM `users_cv5`";
        $res = mysql_query($query);
        if(!$res){
            $report['usernameUsed'] = "Chyba v SQL:".mysql_error();
        }
        while($row=mysql_fetch_assoc($res)){
            if($row['username'] == $login){
                return true;
            }else{
                return false;
            }
        }
    }

    function register($name, $username, $password, $email){
        $md5password = md5($password);
        $query = "INSERT INTO `users_cv5` (`name`, `username`, `password`, `email`) VALUES ('$name','$username','$md5password','$email')";
        $result = mysql_query($query);
        if(!$result){
            $report[insertDB] = "Chyba SQL:".mysql_error();
            return false;
        }else{
            return true;
        }
    }

    function login($username, $password){
        $query = "SELECT `username`, `password` FROM `users_cv5` WHERE `username`='$username'";
        $res = mysql_query($query);
        if(!$res){
            $report['loginFailed'] = "Chyba v SQL:".mysql_error();
        }
        while($row=mysql_fetch_assoc($res)){
            if($row['username'] == $username && $row['password'] == md5($password)){
                return true;
            }else{
                return false;
            }
        }
    }

    function getName($username){
        $query = "SELECT `name` FROM `users_cv5` WHERE `username`='$username'";
        $res = mysql_query($query);
        if(!$res){
            $report['usernameUsed'] = "Chyba v SQL:".mysql_error();
        }else{
            $row=mysql_fetch_assoc($res);
            return $row[name];
        }
    }

    function  getID($username){
        $query = "SELECT `id` FROM `users_cv5` WHERE `username`='$username'";
        $res = mysql_query($query);
        if(!$res){
            $report['usernameID'] = "Chyba v SQL:".mysql_error();
        }else{
            $row=mysql_fetch_assoc($res);
            return $row[id];
        }
    }

    function saveBMI($user_id, $height, $weight, $bmi){
        $query = "INSERT INTO `data_bmi_cv4` ( `user_id` , `height` , `weight` , `bmi` )
                   VALUES ('$user_id', '$height', '$weight', '$bmi')";
        $result = mysql_query($query);
        if(!$result){
            $report[insertDBFail] = "Chyba SQL:".mysql_error();
            return false;
        }else{
            return true;
        }
    }

    function getOwnBMI($user_id){
        $query = "SELECT * FROM `data_bmi_cv4` WHERE `user_id`='$user_id'";
        $res = mysql_query($query);
        if(!$res){
            $report = "Chyba v SQL:".mysql_error();
            return $report;
        }else{
            return $res;
//            while($row=mysql_fetch_assoc($res)){
//                $result['height'] = $row['height'];
//                $result['weight'] = $row['weight'];
//                $result['bmi'] = $row['bmi'];
//            }
//            return $result;
        }
    }


} 